//
//  SettingVC.m
//  Base_App
//
//  Created by Yutian Duan on 2019/5/29.
//  Copyright © 2019年 Wanwin. All rights reserved.
//

#import "SettingVC.h"
#import "ShowWebVC.h"

#import "BaseTableView.h"
#import "ResidenceSettingCell.h"

#define kTitleKey @"titleKey"
#define kShowLine @"showLine"
#define kRightKey @"rightKey"
#define kValue    @"valueKey"


@interface SettingVC () <UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>

@property (nonatomic, strong) UIView *headView;

@property (nonatomic, strong) UIView *footView;

@property (nonatomic, strong) BaseTableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation SettingVC

- (UIView *)headView {
  if (!_headView) {
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth * 0.35)];
    
    
    UIImageView *backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _headView.yt_width, _headView.yt_height)];
    backImageView.backgroundColor = HEXCOLOR(0xf7f8fc);
    [_headView addSubview:backImageView];
    
    CGFloat logoH = 15;
    if (IS_IPHONE5) {
      logoH = 11;
    }

    ///!
    UIImageView *logoImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, logoH * 14, logoH)];
    logoImage.yt_centerX = _headView.yt_width/2;
    [_headView addSubview:logoImage];
    
    UILabel *versionLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, logoImage.yt_bottom + 20, _headView.yt_width - 100, 20)];
    versionLabel.textAlignment = NSTextAlignmentCenter;
    versionLabel.font = [UIFont systemFontOfSize:13.0f];
    versionLabel.textColor = DEF_GRAY;
    [_headView addSubview:versionLabel];
    
    
    ///!
    logoImage.image = [UIImage imageNamed:@"Setting_logo"];

    versionLabel.text = [NSString stringWithFormat:@"%@ ver.%@",YTLocalizedString(@"恒堡会应用程式iOS版本"),[UIDevice yt_appVersion]];
    
   
    
    
    
  }
  return _headView;
  
}

- (UIView *)footView {
  if (!_footView) {
    _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth * 0.3)];
    
    UILabel *serverLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, _footView.yt_width, 30)];
    serverLabel.textColor = [UIColor blackColor];
    serverLabel.font = [UIFont systemFontOfSize:13.0f];
    serverLabel.textAlignment = NSTextAlignmentCenter;
    [_footView addSubview:serverLabel];
    
    UIImageView *phoneImage = [[UIImageView alloc] initWithFrame:CGRectMake(serverLabel.yt_centerX - 60, serverLabel.yt_bottom, 18, 18)];
    phoneImage.contentMode = UIViewContentModeScaleAspectFit;
    [_footView addSubview:phoneImage];
    
    UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(phoneImage.yt_right + 5, phoneImage.yt_top - 1, 120, 20)];
    phoneLabel.textColor = DEF_RED;
    phoneLabel.font = [UIFont systemFontOfSize:20.0f];
    [_footView addSubview:phoneLabel];
    
    serverLabel.text = YTLocalizedString(@"如积分有任何疑问，请致电我们客户服务热线");
    phoneImage.image = [UIImage imageNamed:@"Setting_Phone"];
    phoneLabel.text = @"2180-9350";
    
    
  }
  return _footView;
  
}

- (void)viewDidLoad {
  [super viewDidLoad];

}

- (void)viewWillAppear:(BOOL)animated {
  
  [super viewWillAppear:animated];
  [self setDefaltNavBar];
  [self setStatusBarStyleBack];


}

- (void)createFields {
  [super createFields];
  self.navigationItem.title = YTLocalizedString(@"设定");
  [self createBlackBack];
  
  
  
}

- (void)createViews {
  [super createViews];

  _tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight - NavBarHeight)];
  _tableView.delegate = self;
  _tableView.dataSource = self;
  _tableView.tableHeaderView = self.headView;
  _tableView.tableFooterView = self.footView;
  [_tableView hideLine];
  [self.view addSubview:_tableView];
  
  
  
}

- (void)createConfig {
  
  ///! 获取当前语言环境
  NSString *languageName = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] firstObject];

  if ([languageName containsString:@"zh"]) {
    ///! 繁体中文
    languageName = @"繁体中文";
  } else {
    languageName = @"English";
  }
  
  
  
  NSArray *arr1 = @[
                    @{
                      kTitleKey : YTLocalizedString(@"语言"),
                      kShowLine  : @"0",
                      kRightKey : languageName
                      },
                    @{
                      kTitleKey : @"null",
                      kShowLine  : @"0",
                      kRightKey : @""
                      },
                    @{
                      kTitleKey : YTLocalizedString(@"通知"),
                      kShowLine  : @"1",
                      kRightKey : @"1"
                      },
                    @{
                      kTitleKey : YTLocalizedString(@"定位"),
                      kShowLine  : @"0",
                      kRightKey : @"1"
                      },
                    @{
                      kTitleKey : @"null",
                      kShowLine  : @"0",
                      kRightKey : @""
                      },
                    @{
                      kTitleKey : YTLocalizedString(@"关于"),
                      kShowLine  : @"1",
                      kRightKey : @"arrow"
                      },
                    @{
                      kTitleKey : YTLocalizedString(@"官方网站"),
                      kShowLine  : @"1",
                      kRightKey : @"arrow"
                      },
                    @{
                      kTitleKey : YTLocalizedString(@"应用条款及细则"),
                      kShowLine  : @"1",
                      kRightKey : @"arrow"
                      },
                    @{
                      kTitleKey : YTLocalizedString(@"私隐政策"),
                      kShowLine  : @"0",
                      kRightKey : @"arrow"
                      },
                    @{
                      kTitleKey : @"null",
                      kShowLine  : @"0",
                      kRightKey : @""
                      },
                    @{
                      kTitleKey : YTLocalizedString(@"修改密码"),
                      kShowLine  : @"1",
                      kRightKey : @"arrow"
                      },
                    @{
                      kTitleKey : YTLocalizedString(@"登出"),
                      kShowLine  : @"0",
                      kRightKey : @"arrow"
                      },

  
                    ];
  
  
  
  
  _dataArray = [arr1 mutableCopy];
  [self.tableView reloadData];
}

#pragma mark -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  NSDictionary *dic = self.dataArray[indexPath.row];
  if ([dic[kTitleKey] isEqualToString:@"null"]) {
    return 10;
  } else {
    return ScreenWidth / 8.33;
  }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
  static NSString *identify = @"ResidenceSettingCell";
  
  ResidenceSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
  if (!cell) {
    cell = [[ResidenceSettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
  }
  
  NSDictionary *dic = self.dataArray[indexPath.row];

  cell.titleLabel.hidden = YES;
  cell.lineView.hidden = YES;
  cell.rightLabel.hidden = YES;
  cell.switchButton.hidden = YES;
  cell.arrwoImage.hidden = YES;

  if ([dic[kTitleKey] isEqualToString:@"null"]) {
    cell.contentView.backgroundColor = HEXCOLOR(0xf7f8fc);
    
  } else {
    cell.contentView.backgroundColor = HEXCOLOR(0xffffff);
    cell.titleLabel.text = dic[kTitleKey];
    cell.titleLabel.hidden = NO;
    
    if ([dic[kShowLine] isEqualToString:@"1"]) {
      cell.lineView.hidden = NO;
    }
    
    if ([dic[kRightKey] isEqualToString:@"1"]) {
      cell.switchButton.hidden = NO;
      cell.switchButton.selected = YES;
    } else if ([dic[kRightKey] isEqualToString:@"0"]) {
      cell.switchButton.hidden = NO;
      cell.switchButton.selected = NO;
    } else if ([dic[kRightKey] isEqualToString:@"arrow"]) {
      cell.arrwoImage.hidden = NO;
    } else {
      cell.rightLabel.hidden = NO;
      cell.rightLabel.text = dic[kRightKey];
    }

  }
  
  
  
  return cell;
  
  
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  NSDictionary *dic = self.dataArray[indexPath.row];

  
  if ([dic[kTitleKey] isEqualToString:YTLocalizedString(@"通知")]) {
    //
    NSMutableDictionary *newDic = [dic mutableCopy];
    if ([dic[kRightKey] isEqualToString:@"1"]) {
      newDic[kRightKey] = @"0";
    } else {
      newDic[kRightKey] = @"1";
    }
    [self.dataArray replaceObjectAtIndex:indexPath.row withObject:newDic];
    [tableView reloadData];
  } else if ([dic[kTitleKey] isEqualToString:YTLocalizedString(@"定位")]) {
    NSMutableDictionary *newDic = [dic mutableCopy];
    if ([dic[kRightKey] isEqualToString:@"1"]) {
      newDic[kRightKey] = @"0";
    } else {
      newDic[kRightKey] = @"1";
    }
    [self.dataArray replaceObjectAtIndex:indexPath.row withObject:newDic];
    [tableView reloadData];
  } else if ([dic[kTitleKey] isEqualToString:YTLocalizedString(@"关于")]) {
    ShowWebVC *aboutVC = [[ShowWebVC alloc] init];
    aboutVC.urlStr = @"https://hkhbc.weinui.com/lib/company/paymentinformation";
    [self.navigationController pushViewController:aboutVC animated:YES];
  } else if ([dic[kTitleKey] isEqualToString:YTLocalizedString(@"官方网站")]) {
    ShowWebVC *officialVC = [[ShowWebVC alloc] init];
    officialVC.urlStr = @"https://hkhbc.weinui.com/lib/company/paymentinformation";
    [self.navigationController pushViewController:officialVC animated:YES];
  } else if ([dic[kTitleKey] isEqualToString:YTLocalizedString(@"应用条款及细则")]) {
    ShowWebVC *termsVC = [[ShowWebVC alloc] init];
    termsVC.urlStr = @"https://hkhbc.weinui.com/lib/company/paymentinformation";
    [self.navigationController pushViewController:termsVC animated:YES];

  } else if ([dic[kTitleKey] isEqualToString:YTLocalizedString(@"私隐政策")]) {
    ShowWebVC *privacyVC = [[ShowWebVC alloc] init];
    privacyVC.urlStr = @"https://hkhbc.weinui.com/lib/company/paymentinformation";
    [self.navigationController pushViewController:privacyVC animated:YES];
  } else if ([dic[kTitleKey] isEqualToString:YTLocalizedString(@"修改密码")]) {
    
    
  } else if ([dic[kTitleKey] isEqualToString:YTLocalizedString(@"登出")]) {
    
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:YTLocalizedString(@"提示") message:YTLocalizedString(@"是否登出HBC?") delegate:self cancelButtonTitle:YTLocalizedString(@"取消") otherButtonTitles:YTLocalizedString(@"确认"), nil];
    [alertView show];
    
  }
  
  
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  if (buttonIndex == 1) {
    [[ToolsServer sharedInstance] loginOut];
    [self.navigationController popToRootViewControllerAnimated:YES];
  }
  
}

@end

